package com.jokins.admin.vue.user.action;

import com.alibaba.fastjson2.JSON;
import com.jokins.admin.vue.user.mapper.SysUserMapper;
import com.jokins.admin.vue.user.param.SysUserInfoParam;
import com.jokins.admin.vue.user.vo.SysUserVo;
import com.jokins.admin.vue.user.actioncontext.GetSysUserDoc;
import com.jokins.common.actions.action.ActionContext;
import com.jokins.common.actions.action.BaseAction;
import com.jokins.common.actions.exception.BusinessException;
import com.jokins.common.entry.vue.user.SysUser;
import com.jokins.common.util.SqlTypeConverter;
import com.jokins.common.util.date.DateFormatter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 〈〉<br>
 *
 * @author Mr.Wang
 * @create 2024/9/9
 */
@Slf4j
@Component
public class GetSysUserDocAction extends BaseAction<GetSysUserDoc> {

    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    protected void process(GetSysUserDoc actionContext) throws BusinessException {
        log.info("进入action。。。。。");
        SysUserInfoParam param=actionContext.getParam();
        String userName=param.getUserName()!=null||"".equals(param.getUserName())?null : param.getUserName();
        log.info("Executing selectUserInfo SQL query...");
        List<SysUser> userList = sysUserMapper.selectUserInfo(userName);
        List<SysUserVo> sysUserVoList=new ArrayList<>();
        for (SysUser sysUser : userList) {
            SysUserVo vo=new SysUserVo();
            SqlTypeConverter.convertEntity(sysUser,vo, DateFormatter.DATE_FORMAT_YYYY_MM_DD_HH_MM_SS);
            sysUserVoList.add(vo);
        }
        log.info("Fetched users: {}", sysUserVoList);
        log.info("sysUserVoList:{}", JSON.toJSONString(sysUserVoList));
        actionContext.setResult(ActionContext.RESULT_DOC_KEY,sysUserVoList);
    }
}